package com.sx.pipeline.ui.setting.update

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.cretin.www.cretinautoupdatelibrary.interfaces.AppDownloadListener
import com.cretin.www.cretinautoupdatelibrary.interfaces.MD5CheckListener
import com.cretin.www.cretinautoupdatelibrary.utils.ResUtils
import com.cretin.www.cretinautoupdatelibrary.utils.RootActivity
import com.sx.pipeline.R
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.utils.AndroidUtil
import java.io.File


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 修改昵称/上级
 */
class CustomUpdateActivity : RootActivity() {
    private var tvInfo: TextView? = null
    private var tvUpdate: TextView? = null
    private var tvCancel: TextView? = null
    private var tvVersionName: TextView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_custom_update)
        findView()

        //当是强制更新的时候需要隐藏取消按钮 这个如果你在自定义UI的时候要注意
        if (downloadInfo.isForceUpdateFlag) {
            tvCancel!!.visibility = View.GONE
        } else {
            tvCancel!!.visibility = View.VISIBLE
        }
        //设置版本号
        tvVersionName!!.text = "v" + downloadInfo.prodVersionName
        //设置更新信息
        tvInfo!!.text = downloadInfo.updateLog
    }

    private fun findView() {
        tvInfo = findViewById(R.id.tv_info)
        tvUpdate = findViewById(R.id.tv_update)
        tvCancel = findViewById(R.id.tv_cancel)
        tvVersionName = findViewById(R.id.tv_version_name)

        //设置取消按钮的事件
        tvCancel?.setOnClickListener { //如果你希望取消的同事也取消任务就调用cancelTask方法 否则直接finish就好
            cancelTask()
            finish()
        }

        //开始获取信息并且下载
        tvUpdate?.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                download()
            }
        })
    }

    private fun installAPK() {
        val file = File("https://hulibaba.oss-cn-beijing.aliyuncs.com/apk/hulibaba.apk")
        AndroidUtil.tryInstallApk(this, file)
    }

    override fun obtainDownloadListener(): AppDownloadListener? {
        return object : AppDownloadListener {
            override fun downloading(progress: Int) {
                //设置下载按钮的文案
                tvUpdate!!.text = "正在下载$progress%"
            }

            override fun downloadFail(msg: String) {
                //下载失败的时候重新设置文案
                tvUpdate!!.text ="重新下载"
                Toast.makeText(applicationContext, "安装文件下载失败"+msg, Toast.LENGTH_SHORT).show()
            }

            override fun downloadComplete(path: String) {
                //只需要你设置自定义页面上的文案即可 其他操作sdk会自己处理
                tvUpdate!!.text = "立即更新"
            }

            override fun downloadStart() {
                //下载开始了，设置按钮文案
                tvUpdate!!.text = "立即更新"
            }

            override fun reDownload() {}
            override fun pause() {}
        }
    }

    /**
     * 如果需要知道文件MD5校验结果就重写此方法
     *
     * @return
     */
    override fun obtainMD5CheckListener(): MD5CheckListener? {
        return object : MD5CheckListener {
            override fun fileMd5CheckFail(originMD5: String, localMD5: String) {}
            override fun fileMd5CheckSuccess() {}
        }
    }
}